package com.welike.shibo.service.impl;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.google.gson.JsonObject;
import com.welike.shibo.config.JiGuanConfig;
import com.welike.shibo.eneity.PushBean;
import com.welike.shibo.service.MyJPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 极光推送
 * 封装第三方api相关
 *
 * @author fangyu
 * @version v1.0.0
 * @since 2019-09-01 13:07
 */

@Slf4j
@Service
public class MyJPushServiceImpl implements MyJPushService {

    @Autowired
    private JiGuanConfig jiGuanConfig;

    /**
     * 广播(所有平台, 所有设备, 不支持附加信息)
     *
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushAll(PushBean pushBean) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.all())    // 所有平台
                .setAudience(Audience.all())    // 所有设备
                .setNotification(
                        Notification.newBuilder()
                                .addPlatformNotification(IosNotification.newBuilder()
                                        .setAlert(pushBean.getAlert())
                                        .setBadge(2)
                                        .setSound("default")
                                        .addExtra("from", "JPush")
                                        .build())
                                .build())
//                                .alert(pushBean.getAlert()))   // 设置推送Bean
//                .setMessage(Message.newBuilder()
//                        .setMsgContent(pushBean.getAlert()).setTitle(pushBean.getTitle())
//                        .addExtra("from", "JPush")
//                        .build())
//                .setOptions(Options.newBj
                .build());
    }

    /**
     * ios 广播
     *
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushIOS(PushBean pushBean) {
        IosAlert alert = IosAlert.newBuilder().setTitleAndBody(pushBean.getTitle(), pushBean.getAlert()).build();


        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(2)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .build())
                .build());
    }


    /**
     * ios通过registid推送 (一次推送最多 1000 个)
     *
     * @param pushBean  推送内容
     * @param registids 推送id
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean, Integer pushType,JsonObject jsonObject, String... registids) {
        IosAlert alert = IosAlert.newBuilder().setTitleAndBody(pushBean.getTitle(), pushBean.getAlert()).build();
//        Map<String, String> extras = new HashMap<String, String>(1);
//        extras.put("from", "JPush");

        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                // 按登记id推送
//                .setAudience(Audience.registrationId(registids))
//                 按别名推送
                .setAudience(Audience.alias(registids))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("push_type", pushType)
                                .addExtra("push_content", jsonObject)
                                .setAlert(alert)
                                .setSound("default") // 设置通知声音
                                .setContentAvailable(true) // 设置唤醒
                                .build())
                        .build())
//                .setNotification(Notification.ios(alert, extras)).build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)    // 设置是否生产环境
                        .build())
                .build());
    }

    /**
     * android广播
     *
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }

    /**
     * android通过registid推送 (一次最多1000个)
     *
     * @param pushBean  推送内容
     * @param registids 推送id
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean, String... registids) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }

    /**
     * 按别名推送
     * @param pushBean
     * @param tagId
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean, String tagId) {
        log.info("发送极光推送请求: {}", pushBean);
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .setAudience(Audience.alias(tagId))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }

    /**
     * 通用api推送
     *
     * @param pushPayload 推送实体
     * @return
     */
    @Override
    public boolean sendPush(PushPayload pushPayload) {
        log.info("发送极光推送请求: {}", pushPayload);
        PushResult result = null;
        try {
            result = jiGuanConfig.getJPushClient().sendPush(pushPayload);
            log.info("JPush ==> {}", result);
        } catch (APIConnectionException e) {
            log.error("极光推送连接异常: ", e);
        } catch (APIRequestException e) {
            log.error("极光推送请求异常: ", e);
        }
        if (result != null && result.isResultOK()) {
            log.info("极光推送请求成功: {}", result);
            return true;
        } else {
            log.info("极光推送请求失败: {}", result);
            return false;
        }
    }
}